package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.POJO.Sport;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.SportService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 运动项目库
 */
@RestController
@RequestMapping("/sport")
public class SportController {

    @Reference
    private SportService sportService;

    //分页查询
    @RequestMapping("/getPage")
    public PageResult getPage(@RequestBody QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();//页码
        Integer pageSize = queryPageBean.getPageSize();//每页记录数
        String queryString = queryPageBean.getQueryString();//查询条件
        PageResult pageResult = sportService.pagingQuery(currentPage, pageSize, queryString);
        return pageResult;

    }

    /**
     * 添加
     *
     * @param sport
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @RequestMapping("/addSport")
    public Result addSport(@RequestBody Sport sport) {
        try {
            sportService.add(sport);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SPORT_ADD_FAIL);
        }
        return new Result(true, MessageConstant.SPORT_ADD_SUCCESS);
    }

    /**
     * 编辑
     *
     * @param sport
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @RequestMapping("/edit")
    public Result editSport(@RequestBody Sport sport) {
        try {
            sportService.edit(sport);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SPORT_EDIT_FAIL);
        }
        return new Result(true, MessageConstant.SPORT_EDIT_SUCCESS);
    }

    /**
     * 编辑窗口回显数据
     *
     * @param
     * @return
     */
    @RequestMapping("/findSportById")
    public Result findSportById(@RequestParam("id") Integer id) {
        try {
            Sport sport = sportService.findSportById(id);
            return new Result(true, MessageConstant.SPORT_ECHO_SUCCESS, sport);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SPORT_ECHO_FAIL);
        }

    }


    /**
     * 按照ID删除
     *
     * @param
     * @return
     */
    @RequestMapping("/deleteById")
    public Result deleteById(@RequestParam("id") Integer id) {
        try {
            sportService.deleteById(id);
            return new Result(true, MessageConstant.SPORT_DELETE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SPORT_DELETE_FAIL);
        }

    }
}
